package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.javabean.UsersBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.UsersModel;

public class LoginActivity extends BaseActivity implements View.OnClickListener,RetrofitListener<UsersBean> {
    private int u_id;
     private TextView u_phone,u_pass,reg_link;
    private ImageView wechat_login,qq_login;
    private CheckBox remember_pass;
    private Button login_button;
   private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        u_phone=(TextView) findViewById(R.id.u_name);
        u_pass=(TextView) findViewById(R.id.u_phone);

        remember_pass = (CheckBox) findViewById(R.id.remember_pass);
        reg_link=(TextView) findViewById(R.id.reg_link);

        login_button=(Button) findViewById(R.id.login_button);

        wechat_login=(ImageView) findViewById(R.id.wechat_login);
        qq_login=(ImageView) findViewById(R.id.qq_login);

        /**
         * 记住密码功能没做
         * 以下注释的代码即是
         * 运行报错
         */

//        if (sp.getBoolean("checkboxBoolean", false))
//        {
//            u_phone.setText(sp.getString("u_phone", null));
//            u_pass.setText(sp.getString("u_pass", null));
//            remember_pass.setChecked(true);
//
//        }


    }
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        String resetpwd_edit_name = data.getStringExtra("name");
//        u_phone.setText(resetpwd_edit_name);
//    }

    @Override
    void initEvents() {
        login_button.setOnClickListener(this);
        reg_link.setOnClickListener(this);
    }

    @Override
    void initData() {
        UsersModel usersModel = new UsersModel();
        usersModel.getUserLogin(
                u_phone.getText().toString().trim(),
                u_pass.getText().toString().trim(),this);

    }

    public void onSuccess(UsersBean usersBean, int flag) {
        u_id = usersBean.getU_id();
        if(!"0".equals(usersBean.getU_id())){
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
            Intent intent = new Intent();
            intent.setClass(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            saveUserInfo(u_id);
        }
    }

    @Override
   public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.login_button:
                //处理登录的点击事件
                if("".equals(u_phone.getText().toString())||"".equals(u_pass.getText().toString()))
                    Toast.makeText(LoginActivity.this,"用户名或密码不能为空",Toast.LENGTH_LONG).show();
                else
                    initData();
                break;
            case R.id.reg_link:
                //处理注册的点击事件
                Intent intent = new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
                break;

        }
    }



    @Override
    public void onFailed() {

    }


    public void saveUserInfo(int u_id)
    {
        SharedPreferences sharePreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharePreferences.edit();//获取编辑器
        editor.putString("u_phonee",u_phone.getText().toString());
        editor.putInt("u_id",u_id);
        editor.putString("u_pass",u_pass.getText().toString());
        editor.commit();
    }

}
